﻿/// <reference path="/ExtJs/adapter/ext-base.js"/>
/// <reference path="/ExtJs/ext-all-debug.js"/>
Ext.namespace('FantastPlatform', 'FantastPlatform.CaseManagement');
FantastPlatform.CaseManagement.AutoSearchField = Ext.extend(Ext.form.TwinTriggerField, {

    width: 550,
    oldValue: '',
    trigger1Class: 'x-form-clear-trigger',
    trigger2Class: 'x-form-search-trigger',
    onTrigger1Click: function() { this.setValue(""); this.store.load({ params: { start: 0, limit: 8} }); },
    onTrigger2Click: function() {
        this.store.load({ params: { start: 0, limit: 8} });
    },
    // private
    initEvents: function() {
        FantastPlatform.CaseManagement.AutoSearchField.superclass.initEvents.call(this);
        this.dqTask = new Ext.util.DelayedTask(this.doQuery, this);
        this.el.on("keydown", this.onKeyUp, this);
    },

    onKeyUp: function(e) {
        if (!e.isSpecialKey()) {
            this.lastKey = e.getKey();
            this.oldValue = this.getValue();
            this.dqTask.delay(500);
        }
    },
    doQuery: function() {
        if (this.getValue() != this.oldValue) {
            this.store.load({ params: { start: 0, limit: 8} });
        }
    }
});
FantastPlatform.CaseManagement.BaseInfoSearchPanel = function(config) {
    Ext.apply(this, config);
    var baseInfoStore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: this.url
        }),
        reader: new Ext.data.JsonReader({
            root: 'topics',
            totalProperty: 'totalCount',
            id: 'Id',
            fields: ['Id', 'Code', 'Name']
        }),
        remoteSort: true
    });
    var searchFeild = new FantastPlatform.CaseManagement.AutoSearchField({ store: baseInfoStore });
    searchFeild.on('specialkey', function(f, e) {
        if (e.getKey() == e.ENTER) {
            searchFeild.onTrigger2Click();
        }
    }, searchFeild);
    var typeId = this.typeId;
    var parentCode = this.parentCode;
    var unitCode = "";
    var defaultCode = this.defaultCode;
    baseInfoStore.on('beforeload', function() {
        if (typeId) {
            baseInfoStore.baseParams = { code: searchFeild.getValue() ? searchFeild.getValue() : '', typeId: typeId ,unitCode:unitCode};
        } else if (parentCode) {
            baseInfoStore.baseParams = { code: searchFeild.getValue() ? searchFeild.getValue() : '', parentCode: parentCode,unitCode:unitCode };
        } else if (defaultCode) {
            baseInfoStore.baseParams = { code: searchFeild.getValue() ? searchFeild.getValue() : defaultCode,unitCode:unitCode };
        } else {
            baseInfoStore.baseParams = { code: searchFeild.getValue() ? searchFeild.getValue() : '',unitCode:unitCode };
        }
    });
    var paperBar = new Ext.PagingToolbar({
        store: baseInfoStore,
        pageSize: 8
    });
    var searchGrid = new Ext.grid.GridPanel({
        store: baseInfoStore,
        style: 'margin-top:10px',
        width: 550,
        bbar: paperBar,
        viewConfig: {
            forceFit: true
        },
        height: 228,
        loadMask: { msg: '正在加载数据...' },
        columns: [{
            header: "ID",
            dataIndex: "Id",
            hidden: true
        }, {
            header: "代码",
            dataIndex: "Code",
            sortable: true,
            width: 100
        }, {
            header: "值",
            dataIndex: "Name",
            sortable: true,
            width: 250
}]
        });
        searchGrid.addListener('rowdblclick', rowClickFn);
        var cl = this.onSClick;
        function rowClickFn(grid, rowIndex, e) {
            var node = grid.getSelections()[0];
            cl.call(this, node.get('Name'), node.get('Id'));

        };
        this.items = [searchFeild, searchGrid];
        searchGrid.store.load({ params: { start: 0, limit: 8} });
        FantastPlatform.CaseManagement.BaseInfoSearchPanel.superclass.constructor.call(this, {
            width: 800,
            border: false,
            bodyStyle: 'padding: 5px'
        });
    };
    Ext.extend(FantastPlatform.CaseManagement.BaseInfoSearchPanel, Ext.Panel, {
//        initComponent: function() {
//            FantastPlatform.CaseManagement.BaseInfoSearchPanel.superclass.initComponent.call(this);
//            this.addEvents('selectedValue');
//        }
    });